package cn.tedu.management.portal.controller;


import cn.tedu.management.portal.dto.StaffDTO;
import cn.tedu.management.portal.ex.InvalidParameterException;
import cn.tedu.management.portal.service.IStaffService;
import cn.tedu.management.portal.util.R;
import cn.tedu.management.portal.vo.StaffUpdateVO;
import cn.tedu.management.portal.vo.StaffVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2020-12-08
 */
@RestController
@RequestMapping("/portal/staff")
public class StaffController {

    @Autowired
    IStaffService staffService;

    @RequestMapping("/insert")
    public R<Void> insertStaff(@Valid StaffDTO staffDTO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new InvalidParameterException(errorMessage);
        }

        staffService.insertStaff(staffDTO);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R<Void> deleteById(Integer id){
        staffService.deleteById(id);
        return R.ok();
    }

    //http://localhost:8080/portal/staff/update/show?id=1
    @RequestMapping("/update/show")
    public R<StaffUpdateVO> showStaff(Integer id){
        StaffUpdateVO staffUpdateVO = staffService.showUpdateStaffById(id);
        return R.ok(staffUpdateVO);
    }

    //http://localhost:8080/portal/staff/update/data?id=1&username=利利&phone=15222222222&deptId=66&jobId=66
    @RequestMapping("/update/data")
    public R<Void> updateStaff(@Valid StaffUpdateVO staffUpdateVO,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String errorMessage = bindingResult.getFieldError().getDefaultMessage();
            throw new InvalidParameterException(errorMessage);
        }
        staffService.updateStaff(staffUpdateVO);
        return R.ok();
    }

    //http://localhost:8080/portal/staff/find/list?id=1
    @RequestMapping("/find/list")
    public R<List<StaffVO>> findList(Integer id){
        List<StaffVO> staffVOList = staffService.findList(id);
        return R.ok(staffVOList);
    }


}
